﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DataAccessLayer;
using System.Web;
using System.ServiceModel.Activation;
using IndexMarkWcfService.Admin.Cache;
using System.ServiceModel;
using System.Security.Principal;
using IndexMarkWcfService.Caches;

namespace IndexMarkWcfService
{
    public partial class IndexMarkService : IIndexMarkService
    {
        private IDataProvider context = ProviderFactory.CreateProvider();

        public IEnumerable<DTO.AdmJob> GetAdmJobs()
        {
            return context.GetAdmJobs();
        }

        public void CopyPersonDegrees(int fromIDTerm, int toIDTerm)
        {
            context.CopyPersonDegrees(fromIDTerm, toIDTerm);
        }

        public IEnumerable<DTO.AdmPerson> GetAdmPersons()
        {
            return context.GetAdmPersons();
        }

        public IEnumerable<DTO.AdmTerm> GetAdmTerms()
        {
            return context.GetAdmTerms();
        }

        public IEnumerable<DTO.AdmPosition> GetAdmPositions()
        {
            return context.GetAdmPositions();
        }

        public IEnumerable<DTO.AdmStatus> GetAdmStatuses()
        {
            return context.GetAdmStatuses();
        }

        public IEnumerable<DTO.AdmCategory> GetAdmCategoriesByTerm(int IDTerm)
        {
            return context.GetAdmCategoriesByTerm(IDTerm);
        }

        public IEnumerable<DTO.AdmDepartment> GetUniversityStruct(int IDTerm, bool withRespondents)
        {
            throw new NotImplementedException("В настоящий момент функционирует только загрузка по требованию");
        }

        public IEnumerable<DTO.DTOBase> GetUniversityStructOnDemand(int IDTerm, DTO.AdmDepartment fromDepartment, bool WithRespondents)
        {
            return context.GetUniversityStructOnDemand(IDTerm, fromDepartment, WithRespondents);
        }

        public IEnumerable<DTO.AdmUnit> GetAdmUnits()
        {
            return context.GetAdmUnits();
        }

        public IEnumerable<DTO.AdmSection> GetIndexMarkStruct(int IDTerm, bool withCharacteristics)
        {
            throw new NotImplementedException("В настоящий момент функционирует только загрузка по требованию");
        }

        public IEnumerable<DTO.DTOBase> GetIndexMarkStructOnDemandBySection(int IDTerm, DTO.AdmSection fromSection, bool WithCharacteristics)
        {
            return context.GetIndexMarkStructOnDemand(IDTerm, fromSection, WithCharacteristics);
        }

        public IEnumerable<DTO.DTOBase> GetIndexMarkStructOnDemandByCharacteristic(int IDTerm, DTO.AdmCharacteristic characteristic)
        {
            return context.GetIndexMarkStructOnDemand(IDTerm, characteristic);
        }

        [Cache(typeof(GradationsStructCache))]
        public IEnumerable<DTO.AdmGradation> GetGradationsStruct(int IDTerm)
        {
            return context.GetGradationsStruct(IDTerm);
        }

        public IEnumerable<DTO.DTOBase> GetGradationsStructOnDemand(int IDTerm, DTO.AdmGradation fromGradation)
        {
            return context.GetGradationsStructOnDemand(IDTerm, fromGradation);
        }

        public void SaveAdmUnit(IEnumerable<DTO.AdmUnit> list)
        {
            context.SaveAdmUnit(list);
        }

        public void SaveAdmTerm(IEnumerable<DTO.AdmTerm> list)
        {
            context.SaveAdmTerm(list);
        }

        public void SaveAdmPerson(IEnumerable<DTO.AdmPerson> list)
        {
            context.SaveAdmPerson(list);
        }

        public void SaveAdmJob(IEnumerable<DTO.AdmJob> list)
        {
            context.SaveAdmJob(list);
        }

        public void SaveAdmStatus(IEnumerable<DTO.AdmStatus> list)
        {
            context.SaveAdmStatus(list);
        }

        public void SaveAdmPosition(IEnumerable<DTO.AdmPosition> list)
        {
            context.SaveAdmPosition(list);
        }

        public void SaveAdmCategoriesByTerm(IEnumerable<DTO.AdmCategory> list)
        {
            context.SaveAdmlCategoriesByTerm(list);
        }

        public void SaveIndexMarkStruct(IEnumerable<DTO.DTOBase> list)
        {
            context.SaveIndexMarkStruct(list);
        }

        public void CopyIndexMarkStruct(int fromIDTerm, int toIDTerm)
        {
            context.CopyIndexMarkStruct2(fromIDTerm, toIDTerm);
        }

        public void CopyUniversityStruct(int fromIDTerm, int toIDTerm)
        {
            context.CopyUniversityStruct2(fromIDTerm, toIDTerm);
        }

        public void SaveUniversityStruct(IEnumerable<DTO.DTOBase> list)
        {
            DepartmentTreeCache.Clear();
            context.SaveUniversityStruct(list);
        }

        public void CopyIndexMarkElement(DTO.DTOBase copiedObject, int toIDTerm, DTO.DTOBase targetObject)
        {
            context.CopyIndexMarkElement(copiedObject, toIDTerm, targetObject);
        }

        public void CopyUniversityElement(DTO.DTOBase copiedObject, int toIDTerm, DTO.DTOBase targetObject)
        {
            context.CopyUniversityElement(copiedObject, toIDTerm, targetObject);
        }

        public void CopyGradationStructElement(DTO.DTOBase copiedObject, int toIDTerm, DTO.DTOBase targetObject)
        {
            context.CopyGradationStructElement(copiedObject, toIDTerm, targetObject);
        }

        public IEnumerable<DTO.AdmResponsible> GetAdmResponsibles()
        {
            return context.GetAdmResponsibles();
        }

        public void SaveAdmResponsible(IEnumerable<DTO.AdmResponsible> list)
        {
            context.SaveAdmResponsible(list);
        }

        public IEnumerable<DTO.AdmCharacteristic> GetAdmCharacteristics()
        {
            return context.GetAdmCharacteristics();
        }

        public void SaveAdmCharacteristic(IEnumerable<DTO.AdmCharacteristic> list)
        {
            context.SaveAdmCharacteristic(list);
        }

        public IEnumerable<DTO.AdmDepartment> GetAdmDepartments(AdmDepartmentSortMode sortMode)
        {
            return context.GetAdmDepartments(sortMode);
        }

        public void SaveAdmDepartment(IEnumerable<DTO.AdmDepartment> list)
        {
            context.SaveAdmDepartment(list);
        }

        public IEnumerable<DTO.AdmSection> GetAdmSections()
        {
            return context.GetAdmSections();
        }

        public void SaveAdmSection(IEnumerable<DTO.AdmSection> list)
        {
            context.SaveAdmSection(list);
        }

        public IEnumerable<DTO.AdmGradation> GetAdmGradations()
        {
            return context.GetAdmGradations();
        }

        public IEnumerable<DTO.AdmCategory> GetAdmCategories()
        {
            return context.GetAdmCategories();
        }

        public void SaveAdmGradation(IEnumerable<DTO.AdmGradation> list)
        {
            context.SaveAdmGradation(list);
        }

        public void SaveAdmCategory(IEnumerable<DTO.AdmCategory> list)
        {
            context.SaveAdmCategory(list);
        }

        public void CopyAdmCategoriesByTerm(int fromIDTerm, int toIDTerm)
        {
            context.CopyAdmCategoriesByTerm(fromIDTerm, toIDTerm);
        }

        // AnswerLog
        public IEnumerable<DTO.AdmAnswerLog> GetAnswerLog(DateTime fromDate, DateTime toDate, List<DTO.FilterItem> filters, 
            bool onlySubscribers)
        {
            return context.GetAnswerLog(fromDate, toDate, filters, onlySubscribers);
        }

        public IEnumerable<DTO.AdmAnswerLog> GetAnswerLogDetails(int IDAnswer)
        {
            return context.GetAnswerLog(IDAnswer);
        }

        public void SaveAnswerLog(IEnumerable<DTO.DTOBase> list)
        {
            context.SaveAnswerLog(list);
        }

        public IEnumerable<DTO.AdmAnswerLog> GetAnswerLogTop()
        {
            return context.GetAnswerLog();
        }

        public void ClearAnswerLog()
        {
            context.ClearAnswerLog();
        }
        // ---

        // InputLog
        public IEnumerable<DTO.AdmInputLog> GetInputLog(DateTime fromDate, DateTime toDate, List<DTO.FilterItem> filters)
        {
            return context.GetInputLog(fromDate, toDate, filters);
        }

        public IEnumerable<DTO.AdmInputLog> GetInputLogTop()
        {
            return context.GetInputLog();
        }

        public void SaveInputLog(IEnumerable<DTO.DTOBase> list)
        {
            context.SaveInputLog(list.Cast<DTO.AdmInputLog>());
        }

        public void ClearInputLog()
        {
            context.ClearInputLog();
        }
        // ---

        public void SaveAdmGradationHierarhial(IEnumerable<DTO.DTOBase> list)
        {
            GradationsStructCache.Clear();
            context.SaveAdmGradationHierarhial(list.Cast<DTO.AdmGradation>());
        }

        public IEnumerable<DTO.AdmDepartmentType> GetAdmDepartmentTypes()
        {
            return context.GetAdmDepartmentTypes();
        }

        public void SaveAdmDepartmentTypes(IEnumerable<DTO.AdmDepartmentType> list)
        {
            context.SaveAdmDepartmentTypes(list);
        }

        public void SaveAdmDegree(IEnumerable<DTO.AdmDegree> list)
        {
            context.SaveAdmDegree(list);
        }

        public IEnumerable<DTO.AdmDegree> GetAdmDegrees()
        {
            return context.GetAdmDegrees();
        }

        public IEnumerable<DTO.AdmPersonDegree> GetAdmPersonDegrees(int IDTerm)
        {
            return context.GetAdmPersonDegrees(IDTerm);
        }

        public void SaveAdmPersonDegrees(IEnumerable<DTO.AdmPersonDegree> list)
        {
            context.SaveAdmPersonDegrees(list);
        }

        public string GetNameBySid(string sid)
        {
            try
            {
                return new SecurityIdentifier(sid).Translate(typeof(NTAccount)).ToString();
            }
            catch (IdentityNotMappedException)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Получение флага наличия подписки
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public DTO.SubscriberState CheckSubscribe(string SID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.CheckSubscribe(SID);
        }

        /// <summary>
        /// Получение данных о подписке
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public DTO.SubscriberInfo GetSubscriberInfo(string SID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetSubscriberInfo(SID);
        }

        /// <summary>
        /// Сохранение данных о подписке
        /// </summary>
        /// <param name="subscriberInfo"></param>
        public void SaveSubscriberInfo(DTO.SubscriberInfo subscriberInfo)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            context.SaveSubscriberInfo(subscriberInfo);
        }

        public IEnumerable<DTO.SubscriberInfo> GetAllActivitySubscribers(DateTime fromDate, DateTime toDate)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetAllActivitySubscribers(fromDate, toDate);
        }
    }
}
